package com.schoolvisitors.vo;

import com.alibaba.fastjson2.JSON;
import com.schoolvisitors.entity.Instrument;
import com.schoolvisitors.entity.InstrumentLoan;
import com.schoolvisitors.entity.Room;
import com.schoolvisitors.enums.InstrumentLoanStatusEnum;
import com.schoolvisitors.enums.InstrumentLoanType;
import com.schoolvisitors.utils.TimeUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Data
public class InstrumentLoanVo {
    private Long id;
    private InstrumentLoanStatusEnum status;
    private String statusName;         // 借还状态
    private String realName;   // 借用人姓名
    private String instrumentName; // 仪器名称
    private LocalDateTime borrowTime;   // 借用时间
    private LocalDateTime returnTime;   // 归还时间
    private String borrowRoomText;

    private String floorName;
    private Short floorNumber;
    private String roomSubType;
    private Long roomNumber;
    private List<String> instrumentImgs;


    private String borrowRoomSubType;
    private String borrowFloorName;
    private Long borrowRoomNumber;

    private String returnRoomSubType;
    private String returnFloorName;
    private Long returnRoomNumber;
    private List<String> returnInstrumentImgs;
    private InstrumentLoanType type;

    private String remark;
    // 静态转换方法，接收实体，返回 DTO
    public static InstrumentLoanVo toVo(InstrumentLoan loan) {

        InstrumentLoanVo vo = new InstrumentLoanVo();
        log.info("toVo loan: {}", loan);
        if (loan.getType() == InstrumentLoanType.LIST){
            Instrument instrument = loan.getInstrument();
            Room room = instrument.getRoom();

            log.info(room.toString());
            vo.setFloorName(room.getFloor().getName());
            vo.setRoomNumber(room.getNumber());
            vo.setRoomSubType(room.getSubType());
            vo.setInstrumentName(instrument.getName());
            vo.setInstrumentImgs(List.of(new String[]{instrument.getImgName()}));
            vo.setFloorNumber(room.getFloor().getNumber());

            vo.setBorrowRoomNumber(room.getNumber());
            vo.setBorrowFloorName(room.getFloor().getName());
            vo.setBorrowRoomSubType(room.getSubType());


        } else {
            Room room = loan.getBorrowRoom();
            vo.setFloorName(room.getFloor().getName());
            vo.setRoomNumber(room.getNumber());
            vo.setRoomSubType(room.getSubType());
            vo.setInstrumentName(loan.getInstrumentName());

            vo.setBorrowRoomNumber(room.getNumber());
            vo.setBorrowFloorName(room.getFloor().getName());
            vo.setBorrowRoomSubType(room.getSubType());
            vo.setFloorNumber(room.getFloor().getNumber());

            log.info("{}", loan.getInstrumentImgs());
            List<String> list = List.of(loan.getInstrumentImgs().replace("[", "").replace("]", "").replaceAll(" ", "").split(","));
            log.info("{}", list);
            vo.setInstrumentImgs(list);


        }

        vo.setReturnInstrumentImgs(imgsStrToList(loan.getReturnImgs()));
        Room returnRoom = loan.getReturnRoom();
        vo.setReturnFloorName(returnRoom.getFloor().getName());
        vo.setReturnRoomNumber(returnRoom.getNumber());
        vo.setReturnRoomSubType(returnRoom.getSubType());

        vo.setId(loan.getId());
        vo.setStatusName(loan.getStatus().getDescription());
        vo.setRealName(loan.getUser() != null ? loan.getUser().getRealName() : null);
        vo.setStatus(loan.getStatus());
        vo.setBorrowTime(loan.getBorrowTime());
        vo.setReturnTime(loan.getReturnTime());
        vo.setRemark(loan.getRemark());
        vo.setType(loan.getType());
        return vo;
    }

    private static List<String> imgsStrToList(String imgsStr){
        if (imgsStr == null) return new ArrayList<>();
        return List.of(imgsStr.replace("[", "").replace("]", "").replaceAll(" ", "").split(","));
    }
}
